
export default {
  initFormData (formList, formData) {
    if (formData === undefined) { return this.resetFields(formList) }
    for (let index = 0; index < formList.length; index++) {
      const item = formList[index]
      if (item.prop === undefined) { continue }
      if (typeof formData[item.prop] === 'boolean') { continue }
      // if (typeof formData[item.prop] === 'object') { continue }

      const arrayArr = ['XCheckbox', 'XFiles', 'XFiles2']

      const numberArr = ['XRate', 'XSlider']

      if (!formData[item.prop]) {
        if (numberArr.indexOf(item.type) > -1 || (item.attrs && item.attrs.xType === Number) || item.xType === Number) {
          formData[item.prop] = 0
        } else if (arrayArr.indexOf(item.type) > -1 || (item.attrs && item.attrs.xType === Array) || item.xType === Array) {
          formData[item.prop] = []
        } else {
          formData[item.prop] = undefined
        }
      }
    }
    return this.deepClone(formData)
  },
  resetFields (formList) {
    let formData = {}
    for (let index = 0; index < formList.length; index++) {
      const item = formList[index]
      if (item.prop === undefined) { continue }

      const arrayArr = ['XCheckbox', 'XFiles', 'XFiles2']

      const numberArr = ['XRate', 'XSlider']

      if (numberArr.indexOf(item.type) > -1 || (item.attrs && item.attrs.xType === Number) || item.xType === Number) {
        formData[item.prop] = 0
      } else if (arrayArr.indexOf(item.type) > -1 || (item.attrs && item.attrs.xType === Array) || item.xType === Array) {
        formData[item.prop] = []
      } else {
        formData[item.prop] = undefined
      }
    }
    return formData
  },

  deepClone (source) {
    if (!source && typeof source !== 'object') {
      throw new Error('error arguments', 'deepClone')
    }
    const targetObj = source.constructor === Array ? [] : {}
    Object.keys(source).forEach(keys => {
      if (source[keys] && typeof source[keys] === 'object') {
        targetObj[keys] = this.deepClone(source[keys])
      } else {
        targetObj[keys] = source[keys]
      }
    })
    return targetObj
  }
}
